package com.zuoyy.admin.system.service.impl;

import com.zuoyy.admin.system.service.BaseInterface;
import com.zuoyy.admin.system.service.PostInterface;
import com.zuoyy.common.enums.StatusEnum;
import com.zuoyy.common.utils.EntityBeanUtil;
import com.zuoyy.common.vo.JsonResponse;
import com.zuoyy.modules.system.domain.Post;
import com.zuoyy.modules.system.query.PostQuery;
import com.zuoyy.modules.system.service.PostService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 
 */
@Service
public class PostInterfaceImpl extends BaseInterface implements PostInterface {

    @Autowired
    private PostService postService;


    @Override
    public JsonResponse getListPage(PostQuery query) {
        Page<Post> page = postService.getPageList(query);
        List<Post> list = page.getContent();
        return JsonResponse.success(setQueryResult(query.getDraw(), (int) page.getTotalElements(),list));
    }

    @Override
    public JsonResponse saveOrUpdate(Post domain) {
        if (!StringUtils.isEmpty(domain.getId())) {
            Post post = postService.findById(domain.getId());
            EntityBeanUtil.copyProperties(post, domain);
        }
        postService.save(domain);
        return JsonResponse.success();
    }


    @Override
    @Transactional
    public JsonResponse updateStatus(StatusEnum statusEnum, List<String> ids) {
        if (postService.updateStatus(statusEnum, ids)) {
            return JsonResponse.success(statusEnum.message + "成功");
        } else {
            return JsonResponse.error(statusEnum.message + "失败，请重新操作");
        }
    }





}